local skel = fk.CreateSkill {
  name = "rmt__yixin",
}

Fk:loadTranslationTable{
  ["rmt__yixin"] = "一心",
  [":rmt__yixin"] = "你可以将三张或唯一手牌当【瞒天过海】使用，然后若使目标手牌花色数：1.等于手牌数，令其弃置所有手牌；2.调整至四，你可以摸三张牌并弃置一半手牌。若均满足，以你使用代替弃置。",

  ["#rmt__yixin"] = "一心：将三张或唯一手牌当【瞒天过海】使用，然后根据目标手牌花色数执行后续",
  ["#rmt__yixin-use"] = "一心：你须使用这些牌",
  ["#rmt__yixin-draw-discard"] = "一心:你可以摸三张牌并弃置一半手牌（向上取整）",
  ["#rmt__yixin-draw-use"] = "一心:你可以摸三张牌并使用一半手牌（向上取整）",
  ["#rmt__yixin-chooseuse"] = "一心:选择 %arg 张手牌使用",

  ["$rmt__yixin1"] = "军人都很单纯的，都是为了为国尽忠而生的。",
  ["$rmt__yixin2"] = "他妈的，国家怎么变成了这个样子。",
}

skel:addEffect("viewas", {
  anim_type = "control",
  prompt = "#rmt__yixin",
  card_filter = function (self, player, to_select, selected)
    if not table.contains(player:getHandlyIds(), to_select) then return false end
    local n = player:getHandcardNum()
    if n ~= 1 then n = 3 end
    return #selected < n
  end,
  view_as = function (self, player, cards)
    local n = player:getHandcardNum()
    if n ~= 1 then n = 3 end
    if #cards ~= n then return nil end
    if Fk.all_card_types["underhanding"] == nil then return nil end
    local c = Fk:cloneCard("underhanding")
    c.skillName = skel.name
    c:addSubcards(cards)
    return c
  end,
  before_use = function (self, player, use)
    -- 用于确定使用此牌前哪些目标已经有4种花色，后不会参与“调整至4花色”的结算
    if use.tos then
      use.extra_data = use.extra_data or {}
      use.extra_data.rmt__yixinFourSuit = {}
      for _, to in ipairs(use.tos) do
        local suits = {}
        local hand = to.player_cards[Player.Hand]
        for _, id in ipairs(hand) do
          table.insertIfNeed(suits, Fk:getCardById(id).suit)
        end
        table.removeOne(suits, Card.NoSuit)
        if #suits == 4 then
          table.insertIfNeed(use.extra_data.rmt__yixinFourSuit, to.id)
        end
      end
    end
  end,
  after_use = function (self, player, use)
    local room = player.room
    local tos = {} ---@type ServerPlayer[]
    local fourSuit = (use.extra_data or Util.DummyTable).rmt__yixinFourSuit or Util.DummyTable
    for _, to in ipairs(use.tos) do
      if not to.dead then
        table.insertIfNeed(tos, to)
      end
    end
    if #tos == 0 then return end
    room:sortByAction(tos)
    local myOption
    local optionMap = {}
    for _, to in ipairs(tos) do
      local suits = {}
      local hand = to.player_cards[Player.Hand]
      for _, id in ipairs(hand) do
        table.insertIfNeed(suits, Fk:getCardById(id).suit)
      end
      local option2 = false
      if #suits == 4 and not table.contains(fourSuit, to.id) then -- 令目标花色数调整为4
        if myOption == nil then myOption = "discard" end
        option2 = true
      end
      if #suits == #hand then -- 令目标手牌数等于花色数
        optionMap[to] = (option2 and "use" or "discard")
        if option2 then
          myOption = "use"
        end
      end
    end
    for _, to in ipairs(tos) do
      local option = optionMap[to]
      if option and not to.dead and not to:isKongcheng() then
        room:doIndicate(player, {to})
        if option == "use" then
          local useIds = table.filter(to:getCardIds("h"), function(id) return not player:prohibitUse(Fk:getCardById(id)) end)
          while not player.dead and #useIds > 0 do
            local touse = room:askToUseRealCard(player, {
              skill_name = skel.name, skip = false, prompt = "#rmt__yixin-use", cancelable = false,
              pattern = useIds, expand_pile = useIds,
            })
            if not touse then break end
            useIds = table.filter(useIds, function(id) return table.contains(to.player_cards[Player.Hand], id) end)
          end
        else
          to:throwAllCards("h", skel.name)
        end
      end
    end
    if not player.dead and myOption then
      if room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#rmt__yixin-draw-"..myOption }) then
        player:drawCards(3, skel.name)
        if player.dead or player:isKongcheng() then return end
        local x = math.ceil(player:getHandcardNum() / 2) -- 向上取整
        if myOption == "use" then
          local useIds = room:askToCards(player, {
            min_num = x, max_num = x, include_equip = false,
            skill_name = skel.name, cancelable = false, prompt = "#rmt__yixin-chooseuse:::"..x,
          })
          while not player.dead and #useIds > 0 do
            local touse = room:askToUseRealCard(player, {
              skill_name = skel.name, skip = false, prompt = "#rmt__yixin-use", cancelable = false,
              pattern = useIds,
            })
            if not touse then break end
            useIds = table.filter(useIds, function(id) return table.contains(player.player_cards[Player.Hand], id) end)
          end
        else
          room:askToDiscard(player, {min_num = x, max_num = x, include_equip = false, skill_name = skel.name, cancelable = false})
        end
      end
    end
  end,
  enabled_at_play = function(self, player)
    return not player:isKongcheng()
  end,
  enabled_at_response = Util.FalseFunc,
})

return skel
